package cn.fuxi2022.module.passage.service.impl;

import cn.fuxi2022.core.utils.PageUtils;
import cn.fuxi2022.core.utils.Query;
import cn.fuxi2022.model.empty.applets.GroupPassage;
import cn.fuxi2022.module.passage.mapper.GroupPassageMapper;
import cn.fuxi2022.module.passage.service.GroupPassageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 公众号文章（小程序供用户查看） 服务实现类
 * </p>
 *
 * @author fuxi
 * @since 2022-10-09 10:32:56
 */
@Service
public class GroupPassageServiceImpl extends ServiceImpl<GroupPassageMapper, GroupPassage> implements GroupPassageService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 1.构建搜索条件
        LambdaQueryWrapper<GroupPassage> wrapper = new LambdaQueryWrapper<>();
        // 按照创建日期排序
        wrapper.orderByDesc(GroupPassage::getGmtCreate);

        // 2.获取请求参数，判断是否进行搜索查询
        // 搜索标题或描述
        if (ObjectUtils.isNotEmpty(params.get("search"))) {
            String search = String.valueOf(params.get("search"));
            wrapper.like(GroupPassage::getTitle, search).or().like(GroupPassage::getDescription, search);
        }

        // 搜索状态
        if (ObjectUtils.isNotEmpty(params.get("status"))) {
            int status = Integer.parseInt(String.valueOf(params.get("status")));

            if (status == 0) {
                wrapper.eq(GroupPassage::getStatus, 0);
            } else if (status == 1) {
                wrapper.eq(GroupPassage::getStatus, 1);
            }
        }

        // 搜索类型
        if (ObjectUtils.isNotEmpty(params.get("type"))) {
            int status = Integer.parseInt(String.valueOf(params.get("type")));

            if (status == 0) {
                wrapper.eq(GroupPassage::getType, 0);
            } else if (status == 1) {
                wrapper.eq(GroupPassage::getType, 1);
            } else if (status == 2) {
                wrapper.eq(GroupPassage::getType, 2);
            } else if (status == 3) {
                wrapper.eq(GroupPassage::getType, 3);
            }
        }

        // 3.工具分页
        IPage<GroupPassage> page = this.page(
                new Query<GroupPassage>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }
}
